//
//  UserLoginView.m
//  much
//
//  Created by weijunjie on 2019/1/3.
//  Copyright © 2019年 iOS. All rights reserved.
//

#import "UserLoginView.h"
#import "NSString+Action.h"
#import "MuchUserRequestManager.h"

@interface UserLoginView() <UITextFieldDelegate>
@property (nonatomic, strong) UIImageView *logoImageView;
@property (nonatomic, strong) UITextField *loginTextField;
@property (nonatomic, strong) UIButton *loginButton;
@property (nonatomic, strong) UIButton *agreementButton; // 电商法

@end
@implementation UserLoginView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self loginButton];
        [self agreementButton];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.loginTextField becomeFirstResponder];
        });
    }
    return self;
}

#pragma mark - 懒加载
- (UIImageView *)logoImageView {
    if (!_logoImageView) {
        _logoImageView = [[UIImageView alloc] init];
        _logoImageView.backgroundColor = [UIColor redColor];
        [self addSubview:_logoImageView];
        [_logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(58);
            make.height.mas_equalTo(28);
            make.centerX.equalTo(self);
            make.top.equalTo(self).offset(20);
        }];
    }
    return _logoImageView;
}

- (UITextField *)loginTextField {
    if (!_loginTextField) {
        _loginTextField = [[UITextField alloc] init];
        _loginTextField.keyboardType = UIKeyboardTypeNumberPad;
        _loginTextField.textColor = Color34;
        _loginTextField.font = APPFONT(14);
        _loginTextField.delegate = self;
        _loginTextField.placeholder = @"请输入您的手机号";
        [_loginTextField setValue:Color140 forKeyPath:@"_placeholderLabel.textColor"];
        [self addSubview:_loginTextField];
        [_loginTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(15);
            make.right.equalTo(self).offset(-15);
            make.height.mas_equalTo(20);
            make.top.equalTo(self.logoImageView).offset(100);
        }];
        
        UIView *bottomLineView = [[UIView alloc] init];
        bottomLineView.backgroundColor = Color217;
        [self addSubview:bottomLineView];
        [bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.loginTextField);
            make.height.mas_equalTo(0.5);
            make.top.equalTo(self.loginTextField.mas_bottom).offset(11);
        }];
    }
    return _loginTextField;
}

- (UIButton *)loginButton {
    if (!_loginButton) {
        _loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_loginButton setTitle:@"下一步" forState:UIControlStateNormal];
        [_loginButton setTitleColor:Color140 forState:UIControlStateNormal];
        _loginButton.backgroundColor = Color217;
        _loginButton.titleLabel.font = APPFONT(15);
        _loginButton.userInteractionEnabled = NO;
        [self addSubview:_loginButton];
        [_loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.loginTextField);
            make.height.mas_equalTo(45);
            make.top.equalTo(self.loginTextField.mas_bottom).offset(61);
        }];
        [_loginButton addTarget:self action:@selector(loginButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginButton;
}

- (UIButton *)agreementButton {
    if (!_agreementButton) {
        NSString *text = @"登录即代表您同意《用户服务协议》";
        CGSize textSize = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : APPFONT(12)} context:nil].size;
        UILabel *agreementLabel = [[UILabel alloc] init];
        agreementLabel.font = APPFONT(12);
        agreementLabel.textColor = Color140;
        NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:text];
        NSDictionary *attribtDic = @{NSUnderlineStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
        ;
        [attribtStr addAttributes:attribtDic range:NSMakeRange(8, text.length - 8)];
        agreementLabel.attributedText = attribtStr;
        [self addSubview:agreementLabel];
        [agreementLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self).offset(-30);
            make.left.equalTo(self).offset((ScreenWidth - textSize.width - 18) * 0.5 + 18);
        }];
        
        _agreementButton = [[UIButton alloc] init];
        [_agreementButton setImage:[UIImage imageNamed:@"agreement_select"] forState:UIControlStateSelected];
        [_agreementButton setImage:[UIImage imageNamed:@"agreement_normal"] forState:UIControlStateNormal];
        _agreementButton.selected = YES;
        [self addSubview:_agreementButton];
        
        [_agreementButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(agreementLabel.mas_left).offset(-5);
            make.centerY.equalTo(agreementLabel);
            make.width.height.mas_equalTo(13);
        }];
        
        __weakConfigure
        [_agreementButton tapUpWithBlock:^(UIView *v) {
            UIButton *btn = (UIButton *)v;
            btn.selected = !btn.selected;
        }];
        
        [agreementLabel tapUpWithBlock:^(UIView *v) {
            
        }];
    }
    return _agreementButton;
}

#pragma mark - event
- (void)loginButtonAction {
    [self endEditing:YES];
    if (![self.loginTextField.text isVaildTel]) {
        NSLog(@"手机号有误");
        return;
    }
    
    [self requestCaptchaWithIdentity:self.loginTextField.text];
}

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (string.length == 0) {
        [self loginButtonEnabled:NO];
        return YES;
    }
    if (textField.text.length >= 11) {
        return NO;
    }
    NSString *allStr = [NSString stringWithFormat:@"%@%@",textField.text,string];
    [self loginButtonEnabled:allStr.length == 11];
    return YES;
}

- (void)loginButtonEnabled:(BOOL)enabled {
    if (enabled) {
        self.loginButton.backgroundColor = Color175030;
        [self.loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.loginButton.userInteractionEnabled = YES;
    } else {
        [self.loginButton setTitleColor:Color140 forState:UIControlStateNormal];
        self.loginButton.backgroundColor = Color217;
        self.loginButton.userInteractionEnabled = NO;
    }
}

#pragma mark - request
// 获取验证码
- (void)requestCaptchaWithIdentity:(NSString *)identity {
    [SVProgressHUD showWithStatus:@"加载中..."];
    [MuchUserRequestManager userLoginRequestCaptchaWithIdentity:identity successBlock:^(id responseObject) {
        if (self.login) {
            self.login(self.loginTextField.text);
        }
    } failureBlock:nil];
}

@end
